<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>侧边栏的优化(Optimization of the sidebar)</title>
  <style>
    .tool-bar {
      position: fixed;
      top: 10%;
      right: 0;

      width: 35px;

      display: flex;
      flex-direction: column;
      align-items: center;
    }

    .tool-bar .item {
      position: relative;
      float: right;
      
      margin-bottom: 1px;
      width: 35px;
      height: 35px;

      background-color: #7a6e6e;
      border-radius: 3px 0 0 3px;
    }

    .tool-bar .icon {
      position: absolute;
      right: 0;
      top: 0;
      width: 100%;
      height: 100%;
      cursor: pointer;
      background-image: url(./img/toolbars.png);

      /* 禁止鼠标交互 */
      pointer-events: none;
    }

    .tool-bar .name {
      position: absolute;
      z-index: -1;
      right: 35px;
      top: 0;

      width: 0;
      height: 35px;
      line-height: 35px;
      color: #fff;
      text-align: center;
      font-size: 12px;
      background-color: #7a6e6e;
      cursor: pointer;
      border-radius: 3px 0 0 3px;
      transition: width .2s ease;
    }

    .tool-bar .item:hover,
    .tool-bar .item:hover .name {
      background-color: #cd1926;
    }
  </style>
</head>
<body>

  <div class="tool-bar">
    <div class="item">
      <i class="icon icon_cart"></i>
      <div class="name">购物车</div>
    </div>
    <div class="item">
      <i class="icon icon_favor"></i>
      <div class="name">收藏</div>
    </div>
    <div class="item">
      <i class="icon icon_activity"></i>
      <div class="name">限时活动</div>
    </div>
    <div class="item">
      <i class="icon icon_coupon"></i>
      <div class="name">大礼包</div>
    </div>
  </div>

  <script>
    const toolbarEl = document.querySelector('.tool-bar')
    const iconAllEl = toolbarEl.querySelectorAll('.icon')
    iconItemHandle(iconAllEl)
    
    toolbarEl.onmouseover = function (e) {
      handlMouseEvent(e, 66)
    }

    toolbarEl.onmouseout = function (e) {
      handlMouseEvent(e)
    }

    function handlMouseEvent(e, width=0) {
      if (e.target.className === 'item') {
        const nameEl = e.target.children[1]
        nameEl.style.width = `${width}px`
      }
    }

    function iconItemHandle(domList) {
      for (let i = 0; i < domList.length; i++) {
        domList[i].style.backgroundPosition = `-48px -${i * 50}px`
      }
    }
    
  </script>
  
</body>
</html>